<template>
  <el-form :inline="true" label-position="right" label-width="80px" class="query-form" :style="themeFormStyle">
    <el-form-item v-for="(formItem, index) in formSearch" :key="index" :label="formItem.label">
      <el-input v-if="formItem.elType === 'input'" v-model="conditionsOrigin[index].value" :placeholder="formItem.placeholder?formItem.placeholder:''" :clearable="formItem.clearable" />
      <el-select v-if="formItem.elType === 'select'" v-model="conditionsOrigin[index].value" :placeholder="formItem.placeholder?formItem.placeholder:''" :clearable="formItem.clearable">
        <el-option
          v-for="item in $store.getters['directory/getDirectory'](conditionsOrigin[index].dirField)"
          :key="item.id"
          :label="item.label"
          :value="item.data"
        />
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-button icon="el-icon-search" plain @click="search">搜索</el-button>
    </el-form-item>
    <el-form-item>
      <el-button icon="el-icon-plus" plain @click="addClick">新增</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { clone } from 'lodash'
export default {
  name: 'MySearchForm',
  props: {
    formSearch: Array
  },
  data: function() {
    return {
      conditionsOrigin: []
    }
  },
  computed: {
    themeFormStyle: function() {
      return this.$store.state.settings.themeFormStyle.FORM_SEARCH
    }
  },
  created() {
    if (this.formSearch) {
      for (let index = 0; index < this.formSearch.length; index++) {
        this.conditionsOrigin[index] = {}
        const field = this.formSearch[index].field
        const dirField = this.formSearch[index].dirField ? this.formSearch[index].dirField : field
        this.conditionsOrigin[index].field = field
        this.conditionsOrigin[index].symbol = this.formSearch[index].symbol
        this.conditionsOrigin[index].type = this.formSearch[index].daType
        this.conditionsOrigin[index].dirField = dirField
        if (this.formSearch[index].daType === 'select') {
          this.$store.commit('directory/setDirectory', dirField)
        }
      }
      this.conditionsOrigin = clone(this.conditionsOrigin)
    }
  },
  methods: {
    search: function() {
      this.$emit('search', this.conditionsOrigin)
    },
    addClick: function() {
      this.$emit('addClick')
    }
  }
}
</script>
